package com.nut.course.service.impl;

import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.nut.course.common.Result;
import com.nut.course.entity.PartnerCourse;
import com.nut.course.mapper.BizPartnerCourseMapper;
import com.nut.course.service.PartnerCourseService;
import com.nut.course.util.JacksonUtils;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.StringUtils;

import java.util.List;
import java.util.Set;

@Slf4j
@Service
public class PartnerCourseServiceImpl implements PartnerCourseService {

    @Autowired
    private BizPartnerCourseMapper bizPartnerCourseMapper;

    @Override
    public Result<IPage<PartnerCourse>> list(Integer partnerId, String status, String name, Integer pageNo, Integer pageSize) {
        Page<PartnerCourse> page = new Page<>();
        page.setSize(pageSize);
        page.setCurrent(pageNo);

        String[] statusIds = null;
        if (StringUtils.hasLength(status)) {
            statusIds = status.split(",");
        }

        List<PartnerCourse> list = bizPartnerCourseMapper.selectListPage(page, partnerId, statusIds, name);
        page.setRecords(list);
        return Result.ok(page);
    }

    @Override
    public Result<PartnerCourse> getById(Integer id) {
        PartnerCourse partnerCourse = bizPartnerCourseMapper.selectById(id);
        return Result.ok(partnerCourse);
    }

    @Override
    public Result add(PartnerCourse partnerCourse) {

        List<String> banners = partnerCourse.getBanners();
        if (banners != null && banners.size() > 0) {
            partnerCourse.setBanner(JacksonUtils.toJson(banners));
        }

        bizPartnerCourseMapper.insert(partnerCourse);
        return Result.ok();
    }

    @Override
    public Result updateById(PartnerCourse partnerCourse) {

        List<String> banners = partnerCourse.getBanners();
        if (banners != null && banners.size() > 0) {
            partnerCourse.setBanner(JacksonUtils.toJson(banners));
        }

        bizPartnerCourseMapper.updateById(partnerCourse);
        return Result.ok();
    }

    @Override
    public Result deleteByIds(Set<Integer> ids) {
        for (Integer id : ids) {
            bizPartnerCourseMapper.deleteById(id);
        }
        return Result.ok();
    }

    public static void main(String[] args) {

        String[] strings = {"dd", "ff", "gg"};
        System.out.println(strings);
        System.out.println(strings.toString());

    }
}
